package com.lbh.cloudgallery.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbh.cloudgallery.entity.CartEntity;
import com.lbh.cloudgallery.entity.CartItemEntity;
import com.lbh.cloudgallery.entity.vo.CartItemVo;
import com.lbh.cloudgallery.service.CartItemService;
import com.lbh.cloudgallery.service.CartService;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.Arrays;
import java.util.Map;


@Api(tags = "购物车")
@RestController
@RequestMapping("front/cart")
public class FrontCartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private CartItemService cartItemService;
    /**
     * 添加购物车
     * @return
     */
    @ApiOperation(value = "添加购物车")
    @PostMapping("/addCart")
    public R addCart(@RequestBody CartItemVo vo){
        //判断此用户有没有购物车
        CartEntity cartEntity = cartService.getOne(new QueryWrapper<CartEntity>().eq("member_id", vo.getMemberId()));

        //若没有生成一个购物车
        if(cartEntity==null){
            CartEntity cartEntity1 = new CartEntity();
            cartEntity1.setMemberId(vo.getMemberId());
            cartService.save(cartEntity1);
        }

        //之后存入数据
        CartItemEntity cartItemEntity = cartService.addCart(vo);

        return R.ok().setData(cartItemEntity);
    }



}
